/*
 * @version: 1.2.3
 * @Date: 2022-11-24 17:30:26
 * @LastEditors: 林祥柯
 * @LastEditTime: 2023-01-07 14:58:46
 * @Description: 
 */
import 'package:flutter/material.dart';
// 引入的公共方法和工具
import 'package:happy_elf_app/utils/adaption.dart';
import 'package:happy_elf_app/utils/globalKey.dart';
import 'package:happy_elf_app/utils/themColor.dart';
// 第三方插件引入
import 'package:lottie/lottie.dart';

// noticeText loading提示语
// width modal的宽度设置，有loading提示语时默认为250.px，无提示语时默认为145.px
// isBackHidden 物理返回是否可以关闭弹窗 true/可以 false/不能
void showLoading({
  String? noticeText,
  Widget? loadIcon,
  BuildContext? context,
  bool isBackHidden = false,
  EdgeInsetsGeometry? padding,
}) {
  showDialog(
      context: context ?? Global.navigatorKey.currentContext!,
      barrierDismissible: false, // 是否允许点击遮罩隐藏
      useSafeArea: false, // ios安全区域兼容
      barrierColor: Colors.transparent, // 背景遮罩颜色
      builder: (BuildContext context) {
        return WillPopScope(
            onWillPop: isBackHidden ? null : () async => false,
            child: UnconstrainedBox(
              constrainedAxis: Axis.vertical,
              child: SizedBox(
                width: 150.px,
                child: Loading(
                    noticeText: noticeText,
                    loadIcon: loadIcon,
                    padding: padding),
              ),
            ));
      });
}

void hideLoading({BuildContext? context}) {
  print(
      "关闭loading${Navigator.canPop(context ?? Global.navigatorKey.currentContext!)}");
  if (Navigator.canPop(context ?? Global.navigatorKey.currentContext!)) {
    Navigator.pop(context ?? Global.navigatorKey.currentContext!);
  }
}

Future<DialogRoute> showLoadingByRoute({
  BuildContext? context,
  String? noticeText,
  Widget? loadIcon,
  bool isBackHidden = false,
  EdgeInsetsGeometry? padding,
}) async {
  var myDialogRoute = DialogRoute(
    context: context ?? Global.navigatorKey.currentContext!,
    barrierColor: Colors.transparent, // 背景遮罩颜色
    barrierDismissible: false, // 是否允许点击遮罩隐藏
    useSafeArea: false, // ios安全区域兼容
    builder: (BuildContext context) {
      return WillPopScope(
          onWillPop: isBackHidden ? null : () async => false,
          child: UnconstrainedBox(
            constrainedAxis: Axis.vertical,
            child: SizedBox(
              width: 150.px,
              child: Loading(
                noticeText: noticeText,
                loadIcon: loadIcon,
                padding: padding,
              ),
            ),
          ));
    },
  );

  /// push the dialog route
  Navigator.of(context ?? Global.navigatorKey.currentContext!)
      .push(myDialogRoute);
  return myDialogRoute;
}

void hideLodeingByRoute(
    {required DialogRoute dialogRoute, BuildContext? context}) {
  Navigator.of(context ?? Global.navigatorKey.currentContext!)
      .removeRoute(dialogRoute);
}

// ignore: must_be_immutable
class Loading extends StatelessWidget {
  String? noticeText;
  Widget? loadIcon;
  EdgeInsetsGeometry? padding;
  Loading({
    Key? key,
    this.noticeText,
    this.loadIcon,
    this.padding,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SimpleDialog(
        contentPadding: padding ?? EdgeInsets.all(28.px),
        insetPadding: EdgeInsets.zero,
        backgroundColor: ThemColor.shallowDark,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(8.px))),
        // 阴影参数
        elevation: 0,
        children: [
          this.loadIcon ??
              Lottie.asset('lib/assets/lottie_assets/loading-line.zip',
                  width: 33.px, height: 33.px),
          Container(
            width: 80.vw,
            margin: EdgeInsets.only(top: 14.px),
            child: Text(this.noticeText ?? '加载中...',
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: ThemColor.white,
                )),
          )
        ]);
  }
}
